package com.project.myxima.adapters;


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.project.myxima.R;
import com.ximalaya.ting.android.opensdk.model.word.QueryResult;

import java.util.ArrayList;
import java.util.List;

//an adapter for suggestion data
public class SuggestionAdapter extends RecyclerView.Adapter<SuggestionAdapter.InnerHolder> {

    List<QueryResult> mData = new ArrayList<>();
    private TextView mSuggestionItem_tv;
    private OnItemClickListener mItemClickListener;


    @NonNull
    @Override
    public SuggestionAdapter.InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View item = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_suggestion,parent,false);
        return new InnerHolder(item);
    }

    @Override
    public void onBindViewHolder(@NonNull SuggestionAdapter.InnerHolder holder, int position) {
        //get the textView and set Data to it
        final QueryResult queryResult = mData.get(position);
        mSuggestionItem_tv.setText(queryResult.getKeyword());

        //click event
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick(queryResult.getKeyword());
                }
            }
        });
    }


    @Override
    public int getItemCount() {
        return mData.size();
    }

    public void setData(List<QueryResult> keywordList){
        mData = keywordList;
        notifyDataSetChanged();//after adapter's setData,need this
    }

    public class InnerHolder extends RecyclerView.ViewHolder{
        public InnerHolder(View item) {
            super(item);
            mSuggestionItem_tv = itemView.findViewById(R.id.search_suggestion_tv);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener){
        mItemClickListener = listener;
    }

    public interface OnItemClickListener{
        void onItemClick(String keyword);
    }
}
